/**
 *
 */
package com.dhcc.android.base.common.view;


import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.dhcc.android.base.R;
import com.dhcc.android.base.utils.Constants;
import com.dhcc.android.base.utils.StringUtil;

/**
 * 警告的确认窗口
 *
 * @author pengling
 * @date 2014-11-20
 */
public class AlertDialog extends Dialog {
    private Context context;
    private String alert_text;
    private String confirmButtonText;
    private String cacelButtonText;
    private ClickListenerInterface clickListenerInterface;

    public interface ClickListenerInterface {
        public void doConfirm();

        public void doCancel();
    }

    public AlertDialog(Context context, String alert_text,
                       String confirmButtonText, String cacelButtonText) {
        super(context, R.style.alert_dialog);
        this.context = context;
        this.alert_text = alert_text;
        this.confirmButtonText = confirmButtonText;
        this.cacelButtonText = cacelButtonText;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        init();
    }

    public void init() {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.util_poup_alert, null);
        setContentView(view);
        TextView poup_alert = (TextView) view.findViewById(R.id.poup_alert);
        TextView cancel_tv = (TextView) view.findViewById(R.id.cancel_tv);
        TextView confirm_tv = (TextView) view.findViewById(R.id.confirm_tv);
        poup_alert.setText(alert_text);
        if (StringUtil.isEmpty(confirmButtonText)) {
            view.findViewById(R.id.btn_comfirm).setVisibility(View.GONE);
        } else {
            confirm_tv.setVisibility(View.VISIBLE);
            confirm_tv.setText(confirmButtonText);
        }
        cancel_tv.setText(cacelButtonText);

        view.findViewById(R.id.btn_comfirm).setOnClickListener(
                new clickListener());
        view.findViewById(R.id.btn_cancel).setOnClickListener(
                new clickListener());

        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        lp.width = (int) (d.widthPixels * 0.9); // 高度设置为屏幕的0.6
        dialogWindow.setAttributes(lp);
        setOnKeyListener(new DialogOnKeyListener());
    }

    public void setClicklistener(ClickListenerInterface clickListenerInterface) {
        this.clickListenerInterface = clickListenerInterface;
    }

    private class clickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            int id = v.getId();
            if (id == R.id.btn_comfirm) {
                clickListenerInterface.doConfirm();
            } else if (id == R.id.btn_cancel) {
                clickListenerInterface.doCancel();
            }
        }

    }

    ;

    /**
     * Dialog 监听返回事件
     *
     * @author lizhiting
     */
    private class DialogOnKeyListener implements OnKeyListener {
        @Override
        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0 && Constants.isDialogBack) {
                Constants.isDialogBack = false;
                if (StringUtil.isFastDoubleClick(500)) {
                    return false;
                }
                clickListenerInterface.doCancel();
            }
            return false;
        }

    }
}
